#include "tool_tcp.h"

tool_tcp::tool_tcp(QWidget *parent) : QWidget(parent)
{
    QHBoxLayout *MoveToolLayout = new QHBoxLayout(this);

    LocationWidget = new QWidget(this);

    LocationWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    MoveToolLayout->addWidget(LocationWidget);

    MoveToolLayout->setContentsMargins(0,0,0,0);

    this->setLayout(MoveToolLayout);
}

void tool_tcp::initializeWithData(const QJsonObject &functionDetails)
{
    qDebug() << "Function Details:" << functionDetails;
    // 这里的functionDetails压根没有用到
    QVBoxLayout *LocationLayout = new QVBoxLayout(LocationWidget); // 可替换为具体内容布局

    LocationLayout->setContentsMargins(0,0,0,0);// 让坐标系和左侧按钮一样高

//    QLabel *label = new QLabel("Location Widget", LocationWidget); // 示例内容
    //坐标系 + 选项框
    QWidget* LocationSystemWidget = new QWidget(LocationWidget);
    QHBoxLayout *LocationSystemLayaout = new QHBoxLayout(LocationSystemWidget);
    LocationSystemLayaout->setContentsMargins(0,0,0,0);

    QLabel *label = new QLabel("坐标系:", LocationSystemWidget); // 示例内容
    LocationSystemLayaout->addWidget(label);
    // 创建 QComboBox，并添加到 LocationSystemWidget 的布局中
    QComboBox *comboBox = new QComboBox(LocationSystemWidget);
    QStringList items = {"Option 1", "Option 2", "Option 3"};
    comboBox->addItems(items);
    LocationSystemLayaout->addWidget(comboBox);

    // 连接 QComboBox 信号和槽函数
    connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
               this, &tool_tcp::onComboBoxChanged);

    LocationLayout->addWidget(LocationSystemWidget);  // 将 LocationSystemWidget 添加到 LocationLayout

    LocationSystemWidget->setLayout(LocationSystemLayaout); // 设置 LocationSystemWidget 的布局


//  px py pz 以及内容
    QStringList stringList;
    QWidget* LocationTextWidget = new QWidget(LocationWidget);
    QVBoxLayout *LocationTextLayaout = new QVBoxLayout(LocationTextWidget);

    // 向列表中添加字符串
    stringList << "Px:" << "Py:" << "Pz:" << "Rx:"<< "Ry:" << "Rz:";

    for (const QString &item : stringList) {
        QWidget* TextWidget = new QWidget(LocationTextWidget);
        QHBoxLayout *TextLayaout = new QHBoxLayout(TextWidget);
        TextLayaout->setContentsMargins(0,0,0,0);
        QLabel *label = new QLabel(item, TextWidget);
        TextLayaout->addWidget(label);
        QLineEdit* line = new QLineEdit(TextWidget);
        TextLayaout->addWidget(line);
        QLabel *label2 = new QLabel("rad", TextWidget);
        TextLayaout->addWidget(label2);
        TextWidget->setLayout(TextLayaout);
        LocationTextLayaout->addWidget(TextWidget);

        // 将 QLineEdit 添加到 QMap 中，key 为坐标名称
        lineEditsMap.insert(item, line);
    }
    LocationTextWidget->setLayout(LocationTextLayaout);

    LocationLayout->addWidget(LocationTextWidget);

    // 最下面的按钮框
    QWidget* LocationPtnWidget = new QWidget(LocationWidget);
    QHBoxLayout *LocationPtnLayaout = new QHBoxLayout(LocationPtnWidget);
    LocationPtnLayaout->setContentsMargins(0,0,0,0);
    QPushButton *set_ptn = new QPushButton(LocationPtnWidget);
    set_ptn->setText("设置");
    connect(set_ptn, &QPushButton::clicked, this, [this](){
        qDebug() << "设置按钮被点击";

        // 遍历 lineEditsMap 获取每个 QLineEdit 的值
        for (auto it = lineEditsMap.begin(); it != lineEditsMap.end(); ++it) {
            QString label = it.key();
            QString value = it.value()->text();
            qDebug() << label << ":" << value;
        }
    });

    QPushButton *move_ptn = new QPushButton(LocationPtnWidget);
    move_ptn->setText("Move");
    connect(move_ptn, &QPushButton::clicked, this, [this](){
        qDebug() << "Move 按钮被点击";
        // 遍历 lineEditsMap 获取每个坐标名对应的 QLineEdit 的值
        for (auto it = lineEditsMap.begin(); it != lineEditsMap.end(); ++it) {
            QString label = it.key();  // 获取坐标名，如 "Px", "Py", "Pz"
            QString value = it.value()->text();  // 获取对应 QLineEdit 的值
            qDebug() << label << ":" << value;
        }
    });

    QPushButton *io_ptn = new QPushButton(LocationPtnWidget);
    io_ptn->setText("IO");
    connect(io_ptn, &QPushButton::clicked, this, [this](){
        qDebug() << "IO 按钮被点击";
        // 遍历 lineEditsMap 获取每个坐标名对应的 QLineEdit 的值
        for (auto it = lineEditsMap.begin(); it != lineEditsMap.end(); ++it) {
            QString label = it.key();  // 获取坐标名，如 "Px", "Py", "Pz"
            QString value = it.value()->text();  // 获取对应 QLineEdit 的值
            qDebug() << label << ":" << value;
        }
    });

    LocationPtnLayaout->addWidget(set_ptn);
    LocationPtnLayaout->addWidget(move_ptn);
    LocationPtnLayaout->addWidget(io_ptn);

    LocationPtnWidget->setLayout(LocationPtnLayaout);

    LocationLayout->addWidget(LocationPtnWidget);
}

void tool_tcp::onComboBoxChanged(int index)
{
    // 输出选择的索引
        qDebug() << "ComboBox index changed to:" << index;

        // 可以在这里根据选中的索引执行不同的操作
        switch (index) {
            case 0:
                qDebug() << "Option 1 selected";
                // 执行 Option 1 的操作
                break;
            case 1:
                qDebug() << "Option 2 selected";
                // 执行 Option 2 的操作
                break;
            case 2:
                qDebug() << "Option 3 selected";
                // 执行 Option 3 的操作
                break;
            default:
                break;
        }
}

void tool_tcp::connectSignals(QWidget *widget)
{

}

void tool_tcp::setupLayout(QWidget *widget)
{

}
